﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Diagnostics;

public class render : MonoBehaviour {
	public float Range = 5f;
	private string ImgUrl;
	public GameObject[] OldActive;
	public List<GameObject> NewActive;

	public GameObject InUseObject;
	public bool ExpandedImage;
	public bool OpenText;
	public bool MusicPlaying;
	public bool MoviePlaying;


	void Start () {
	
	}
	void Update () {
		Resources.UnloadUnusedAssets();
		//clear all the old ones
		NewActive.Clear();
		//find the new inrange objects
		Collider[] Temp = Physics.OverlapSphere(this.transform.position,Range);
		foreach(Collider C in Temp){
			if(C.gameObject.name == "RenderPlane"){
				GameObject TempGO = C.gameObject.transform.parent.parent.gameObject;
				TempGO.transform.GetChild(0).transform.renderer.enabled = true;
				//if they have a box class on them
				if(TempGO.GetComponent<Box>()){
					string TempStr = TempGO.GetComponent<Box>().StrPath;
					ParseExtConst(TempStr,TempGO);
					//check them for a match in the go list (just incase)
					bool HasMatch = false;
					foreach(GameObject GO in NewActive){
						if(GO.GetComponent<Box>().StrPath == TempStr){
							HasMatch = true;
						}
					}
					if(!HasMatch){
						NewActive.Add(TempGO);
					}
					TempGO.transform.GetChild(0).transform.rotation = Quaternion.LookRotation(TempGO.transform.GetChild(0).transform.position-Camera.main.transform.position,Camera.main.transform.up);
					TempGO.transform.GetChild(1).transform.rotation = Quaternion.LookRotation(TempGO.transform.GetChild(1).transform.position-Camera.main.transform.position,Camera.main.transform.up);
				}
			}
		}

		//tell all the old boxes to unload and clear incase there not in the new cast
		for(int Index1 = 0; Index1 < OldActive.Length; Index1++){
			bool IsInNewList = false;
			for(int Index2 = 0; Index2 < NewActive.Count; Index2++){
				//if the last frames list of gameobjects @ index1 of it isnt
				// in the new list at all unload it
				if(OldActive[Index1] == NewActive[Index2]){
					IsInNewList = true;
				}
			}
			if(!IsInNewList){
				print("GO not in new list Run Unload");
				if(OldActive[Index1]){
					OldActive[Index1].GetComponent<Box>().Unload();
					OldActive[Index1].transform.GetChild(0).transform.renderer.enabled = false;
				}
				if(InUseObject == OldActive[Index1]){
					InUseObject = null;
					ExpandedImage = false;
					OpenText = false;
				}
			}
		}
		OldActive = new GameObject[NewActive.Count];
		NewActive.CopyTo(OldActive);
	}
	//function that can be called every frame for parsing objects around you
	public void ParseExtConst(string StrInput, GameObject GOInput){
		string Ext = (StrInput.Substring(StrInput.Length-4)).ToLower();
		//print ("File Ext:"+Ext);
		if(Ext == ".png" || Ext == ".jpg" || Ext == ".gif"){
			//is it busy loading and does it have a image allready
			if(!GOInput.GetComponent<Box>().IsLoading && !GOInput.GetComponent<Box>().HasImage){
				StartCoroutine(GOInput.GetComponent<Box>().LoadImage());
			}
		}



	}
	//function for activating objects around you Once
	public void ParseExtOnce(string StrInput, GameObject GOInput){
		string Ext = (StrInput.Substring(StrInput.Length-4)).ToLower();
		print ("File["+StrInput+"]");
		if(Ext == ".png" || Ext == ".jpg" || Ext == ".gif"){
			//is it busy loading something
			if(!GOInput.GetComponent<Box>().IsLoading && !GOInput.GetComponent<Box>().Expanded){
				StartCoroutine(GOInput.GetComponent<Box>().LoadImage());
				GOInput.GetComponent<Box>().Expand = true;
				if(ExpandedImage){
					InUseObject.GetComponent<Box>().Contract = true;
					InUseObject.GetComponent<Box>().ContractResize = true;
				}
				ExpandedImage = true;
				InUseObject = GOInput;
			}
			else if(!GOInput.GetComponent<Box>().IsLoading && GOInput.GetComponent<Box>().Expanded){
				GOInput.GetComponent<Box>().Contract = true;
				GOInput.GetComponent<Box>().ContractResize = true;
				InUseObject = null;
				ExpandedImage = false;

			}
		}
		else if(Ext == ".exe"){
			System.Diagnostics.Process.Start(StrInput);
		}
		else if(Ext == ".txt" || Ext == ".ini" || Ext == ".log" || Ext == ".rtf" || Ext == ".doc" || Ext == ".asm" || Ext == ".ps1" || Ext == ".nfo" || Ext == ".lnk" || Ext == ".xml"){
			if(!OpenText){
				GOInput.GetComponent<Box>().FileText = File.ReadAllText(GOInput.GetComponent<Box>().StrPath);
				GOInput.GetComponent<Box>().RenderText = true;
				InUseObject = GOInput;
				OpenText = true;
			}

		}
		
		
	}






}
